/*
 * Hyuga: A Density-Grid Stream Clustering Platform.
 * Copyright (C) 2014 PUC-Rio/Laboratory for Advanced Collaboration
 *
 * Hyuga is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Hyuga is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hyuga.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.pucrio.inf.lac.konoha.hyuga.events.cell;

import br.pucrio.inf.lac.konoha.hyuga.events.CEPEvent;
import br.pucrio.inf.lac.konoha.hyuga.events.LocationUpdateEvent;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * This event is triggered when a location update change (or move away) from a cell  to another one. The recheck event
 * is composed by context partition (cell) its neighboring cells. We will use this event as input to check if the cell
 * is still dense.
 *  
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 */
public class CellRecheckEvent {
    // Cell Core
    protected LocationUpdateEvent[] rawCore;
        
    // Neighbor cells
    private CellContentEvent[] neighbors;

    // Empty Constructor
    public CellRecheckEvent() {}
    
    // Basic Constructor
    public CellRecheckEvent(LocationUpdateEvent[] rawCore) {
        this(rawCore, null);
    }

    /**
     * Construct a new Cell Recheck Event.
     * 
     * @param core the core "dense" cell 
     * @param neighbors the cell neighboring cells
     */
    public CellRecheckEvent(LocationUpdateEvent[] rawCore, CellContentEvent[] neighbors) {
        this.rawCore   = rawCore;
        this.neighbors = neighbors;
    }
    
    /**
     * Retrieve the number of mobile nodes in the cell cluter, that is, the sum of the mobile nodes mapped to the core
     * cell and its neighbor cell.
     * 
     * @return the number of mobile nodes in the cell cluster.
     */
    public int size() {
        int size = 0;
        if (rawCore != null) size += rawCore.length;
        
        if (neighbors != null) {
            for (CellContentEvent neighbor : neighbors) {
                size += neighbor.size();
            }
        }
        
        return size;
    }
    
    @Override
    public String toString() {
        return "(" + rawCore[0].getX() + ", " + rawCore[0].getY() + ") ~ " + size();
    }

    //////////////////////////////////////////////////////////////////////////
    // Mapping functions (utilities)
    //////////////////////////////////////////////////////////////////////////
    public int getX() {
        if (rawCore != null) {
            return rawCore[0].getX();
        } else {
            return -1;
        }
    }
    
    public int getY() {
        if (rawCore != null) {
            return rawCore[0].getY();
        } else {
            return -1;
        }
    }
    
    public CellContentEvent getCoreCell() {
        if (rawCore != null) {
            return new CellContentEvent(rawCore[0].getX(), rawCore[0].getY(), rawCore);    
        } else {
            return new CellContentEvent();
        }
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Gets and Sets
    //////////////////////////////////////////////////////////////////////////
    
    public LocationUpdateEvent[] getRawCore() {
        return rawCore;
    }

    public void setRawCore(LocationUpdateEvent[] rawCore) {
        this.rawCore = rawCore;
    }
    
    public CellContentEvent[] getNeighbors() {
        return neighbors;
    }
    
    public void setNeighbors(CellContentEvent[] neighbors) {
        this.neighbors = neighbors;
    }

}
